﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using Ebada.UI.Base;
using Ebada.Client;
using Ebada.Core;
using Ebada.Client.Platform;
namespace Ebada.Modules.Vehicle
{
    /// <summary>
    /// 违章记录编辑
    /// </summary>
    public partial class frmCardRechargeEdit : FormBase, IPopupFormEdit {
       

        public frmCardRechargeEdit() {
            InitializeComponent();
        }
        void dataBind() {

            this.txtGasCard_Num.DataBindings.Add("EditValue", rowData, "GasCard_Num");
            this.txtRechargeDate.DataBindings.Add("EditValue", rowData, "RechargeDate");
            this.txtRechargePrice.DataBindings.Add("EditValue", rowData, "RechargePrice");
            this.txtEmployedID.DataBindings.Add("EditValue", rowData, "EmployedID");
           
        }
        #region IPopupFormEdit Members
        private RechargeHis rowData = null;

        public object RowData {
            get {
                return rowData;
            }
            set {
                if (value == null) return;
                if (rowData == null) {
                    this.rowData = value as RechargeHis;
                    dataBind();
                    InitComboBoxData();
                } else {
                    ConvertHelper.CopyTo<RechargeHis>(value as RechargeHis, rowData);
                    InitComboBoxData();
                }
            }
        }

        #endregion

        public void InitComboBoxData() {
            IList<Empolyees> list = MainHelper.GetSqlMap<Empolyees>().GetListByWhere<Empolyees>(" where 1=1 order by Empolyee_ID");
            txtEmployedID.Properties.DataSource = list;
            IList<VehicleGasCard> list2 = MainHelper.GetSqlMap<VehicleGasCard>().GetListByWhere<VehicleGasCard>(" where 1=1 order by GasCard_Num");
            txtGasCard_Num.Properties.DataSource = list2;
            rowData.RechargeDate = DateTime.Now;
        }
 
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtGasCard_Num.Text == "")
            {
                MessageBox.Show("油卡卡号不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtGasCard_Num.Focus();
                return;
            }
            if (txtRechargeDate.Text == "")
            {
                MessageBox.Show("充值日期不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtRechargeDate.Focus();
                return;
            }
            if (txtEmployedID.Text == "")
            {
                MessageBox.Show("充值人不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtEmployedID.Focus();
                return;
            }
            decimal val = 0;
            //IList<RechargeHis> objlist = MainHelper.GetSqlMap<RechargeHis>().GetListByWhere<RechargeHis>(" where GasCard_Num='" + txtGasCard_Num.Text + "'");
            //for (int i = 0; i < objlist.Count;i++ )
            //{
            //    val = val+objlist[i].RechargePrice;
            //}
            val = val + txtRechargePrice.Value;
            IList<VehicleGasCard> cardlist = MainHelper.GetSqlMap<VehicleGasCard>().GetListByWhere<VehicleGasCard>(" where GasCard_Num='" + txtGasCard_Num.Text + "'");
            if(cardlist.Count>0){
                VehicleGasCard card = cardlist[0];
                card.TatalPrice =card.TatalPrice+ val;
                MainHelper.GetSqlMap<VehicleGasCard>().Update<VehicleGasCard>(card);
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtEmployedID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            Ebada.Client.Platform.Selector.frmEmpSelectorByOrgInfo frm = new Ebada.Client.Platform.Selector.frmEmpSelectorByOrgInfo();
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.InitionData(string.Format(" where 1=1", this.txtEmployedID.EditValue), false, false);
            if (frm.ShowDialog() == DialogResult.OK)
            {
               // txtEmployedID.Text = frm.CurrentEmp.Empolyee_Name;
                txtEmployedID.EditValue = frm.CurrentEmp.Empolyee_ID;
            }
        }

        private void txtGasCard_Num_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

            frmCardSelector frm = new frmCardSelector();

            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtGasCard_Num.Text = frm.SelectObj.GasCard_Num;
                txtGasCard_Num.ClosePopup();
            }
        }

    }
}